/**------------------------------------------------------------
 *
 * Copyright © 2014 rubbervalley. All rights reserved.
 *
 * @Title: PlatStoreContactController.java
 * @Package: com.rubbervalley.uyo.actions
 * @Description: 公司店铺联系人
 * @author: zhoulei
 * @date: 上午10:35:28
 * @version: V1.0
 * 
 * Update History:
 * 
 ------------------------------------------------------------*/
package com.rubbervalley.uyo.actions.administrator;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rubbervalley.framework.core.common.MessageUtil;
import com.rubbervalley.framework.core.common.ObjectMessages;
import com.rubbervalley.platform.business.PlatStoreContactService;
import com.rubbervalley.platform.domain.PlatStore;
import com.rubbervalley.platform.domain.PlatStoreContact;

@Controller
@RequestMapping(value = "/uyo")
public class PlatStoreContactController {
	@Autowired
	private PlatStoreContactService platStoreContactService;

	@Autowired
	private MessageUtil messageUtil;

	private static final Logger logger = LoggerFactory
			.getLogger(PlatStoreContactController.class);

	@RequestMapping(value = "/platStoreContacts", method = RequestMethod.GET)
	String platStoreContacts(Map<String, Object> model)
	{
		logger.debug("PlatStoreContactController");
		return "/uyo/administrator/platStoreContact/platStoreContacts.ftl";
	}

	@RequestMapping(value = "/platStoreContact/{id}", method = RequestMethod.GET)
	public String detail(Map<String, Object> model,
			@PathVariable("id") Integer id)
	{
		// TODO: 初始化
		PlatStoreContact bean = platStoreContactService.select(id);
		model.put("bean", bean);
		return "/uyo/administrator/platStoreContact/detail.ftl";
	}

	public void validate(PlatStoreContact platStoreContact, Errors errors)
	{
		// TODO: 校验
		// ValidationUtils.rejectIfEmpty(errors, "name", "user.name.required",
		// "name的内容不能为空");
		// errors.reject("system.resource.update.success");
		// errors.rejectValue("name", "名称不正确。");

	}

	@RequestMapping(value = "/platStoreContact/add", method = RequestMethod.GET)
	public String add(Map<String, Object> model)
	{
		// TODO: 初始化
		return "/uyo/administrator/platStoreContact/add.ftl";
	}

	@RequestMapping(value = "/platStoreContact/add", method = RequestMethod.POST)
	@ResponseBody
	public ObjectMessages add(Map<String, Object> model,
			PlatStoreContact platStoreContact, BindingResult result)
	{
		// TODO:修改代码
		validate(platStoreContact, result); // 数据校验
		if (!result.hasErrors()) {
			// TODO:校验失败
			return messageUtil.messages("platStoreContact.add.error");
		} else {
			this.platStoreContactService.insert(platStoreContact);
			// TODO:成功处理
			return messageUtil.messages("platStoreContact.add.success");
		}

	}

	@RequestMapping(value = "/platStoreContact/{id}/update", method = RequestMethod.GET)
	public String update(Map<String, Object> model,
			@PathVariable("id") Integer id)
	{
		// TODO: 初始化
		PlatStoreContact bean = platStoreContactService.select(id);
		model.put("bean", bean);
		return "/uyo/administrator/platStoreContact/update.ftl";
	}

	@RequestMapping(value = "/platStoreContact/{id}/update", method = RequestMethod.POST)
	@ResponseBody
	public ObjectMessages update(Map<String, Object> model,
			PlatStoreContact platStoreContact, BindingResult result)
	{
		// TODO:修改代码
		validate(platStoreContact, result); // 数据校验
		if (result.hasErrors()) {
			// TODO:校验失败
			return messageUtil.messages("platStoreContact.update.error");
		} else {
			this.platStoreContactService.update(platStoreContact);
			// TODO:成功处理
			return messageUtil.messages("platStoreContact.update.success");
		}

	}

	@RequestMapping(value = "/platStoreContact/{id}/delete")
	@ResponseBody
	public ObjectMessages delete(Map<String, Object> model,
			@PathVariable("id") Integer id)
	{
		// TODO:代码
		this.platStoreContactService.delete(id);
		return messageUtil.messages("platStoreContact.delete.success");

	}
	
	/***
	 * 联系人信息编辑
	 * 
	 */
	@RequestMapping(value = "/platStoreContact/contactEdit")
	String storeContact(Map<String, Object> model,PlatStore elem, BindingResult result)
	{
		if(elem.getId()!=null){			//若是企业信息修改，查找已有联系人
			List<PlatStoreContact> contacts = platStoreContactService.selectByStore(elem.getId());
			if(contacts.size()>0){				
				model.put("contacts", contacts.get(0));
			}
		}
		return "/uyo/administrator/platStoreContact/contactEdit.ftl";
	}
	/***
	 * 联系人详情
	 * 
	 */
	@RequestMapping(value = "/platStoreContact/contactInfo")
	String storeContactInfo(Map<String, Object> model,PlatStore elem, BindingResult result)
	{
		if(elem.getId()!=null){			
			List<PlatStoreContact> contacts = platStoreContactService.selectByStore(elem.getId());
			if(contacts.size()>0){
			    model.put("contacts", contacts.get(0));
			}
		}
		return "/uyo/administrator/platStoreContact/contactInfo.ftl";
	}
}